package com.lse_api.control;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lse_api.entity.ProjectApi;
import com.lse_api.entity.UserInfo;
import com.lse_api.service.ProjectApiService;
import com.lse_api.service.UserInfoService;
import com.lse_api.utils.PageUtil;
import com.lse_api.utils.ReturnMsg;
import com.lse_api.utils.Status;
import com.lse_api.utils.pdfutil.stringutil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.lse_api.exception.LseException;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 项目内部API接口 前端控制器
 * </p>
 *
 * @author 祁雪
 * @since 2020-05-11
 */
@Controller
@RequestMapping("/lseapi/api/projectApi")
public class ProjectApiController extends LseException implements ReturnMsg {


    @Autowired
    private HttpServletRequest request;


    @Autowired
    private ProjectApiService projectApiService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 筛选API列表
     * project: 项目ID
     * name: api名称 模糊查询 如未填写则不添加此判断
     * method: 方法数组 传入多个method
     * create: api创建者UID数组
     * pageNo: 页码
     * pageSize: 每页数量
     * <p>
     * 参考where: select * from project_api as api
     * where api.project_id = 7 and (
     * api.name like '%%' and
     * api.method in ('GET','POST') and
     * api.api_create in ('')
     * )
     *
     * @return
     */
    @RequestMapping(value = "/filterApi")
    @ResponseBody
    public Status filterApi() {

        String project = request.getParameter("project");

        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");

        Map<String, Integer> pageMap = PageUtil.getPage(pageNo, pageSize);

        if (StringUtils.isEmpty(project)) {

            return Status.error(API_PROJECT_ID);

        }
        String name = request.getParameter("name");

        String[] method = request.getParameterValues("method[]");

        String[] create = request.getParameterValues("create[]");

        Map<String, Object> map = new HashMap<>();
        map.put("projectId", project);
        map.put("name", name);
        map.put("method", method != null && method.length != 0 ? method : null);
        map.put("create", create != null && create.length != 0 ? create : null);

        PageHelper.startPage(pageMap.get("pageNo"), pageMap.get("pageSize"));
        List<ProjectApi> list = projectApiService.filterApi(map);

        PageInfo<ProjectApi> pageInfo = new PageInfo<>(list);

        return Status.success(pageInfo);
    }

    /**
     * 读取当前项目API创建者列表
     *
     * @return
     */
    @RequestMapping(value = "/getApiCreateList")
    @ResponseBody
    public Status getApiCreateList() {

        String project = request.getParameter("project");
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");

        if (StringUtils.isEmpty(project)) {
            return Status.error(API_PROJECT_ID);
        }

        Map<String, Integer> map = PageUtil.getPage(pageNo, pageSize);
        PageHelper.startPage(map.get("pageNo"), map.get("pageSize"));

        List<UserInfo> list = userInfoService.getUserInfoByProject(project);

        PageInfo<UserInfo> pageInfo = new PageInfo<>(list);

        return Status.success(pageInfo);

    }

}

